package multiprocessor;

import java.util.concurrent.Semaphore;

//todo use other method
public class PrintInOrder1114 {

    Semaphore semaphore2 = new Semaphore(0);
    Semaphore semaphore3 = new Semaphore(0);
    public PrintInOrder1114() {

    }

    public void first(Runnable printFirst) throws InterruptedException {

        // printFirst.run() outputs "first". Do not change or remove this line.
        printFirst.run();
        semaphore2.release();
    }

    public void second(Runnable printSecond) throws InterruptedException {

        // printSecond.run() outputs "second". Do not change or remove this line.
        semaphore2.acquire();
        printSecond.run();
        semaphore3.release();
    }

    public void third(Runnable printThird) throws InterruptedException {
        semaphore3.acquire();
        // printThird.run() outputs "third". Do not change or remove this line.
        printThird.run();
    }







    //method1 use flag
//    private volatile boolean flagA;
//    private volatile boolean flagB;
//    public PrintInOrder1114() {
//        flagA = false;
//        flagB = false;
//    }
//
//    public void first(Runnable printFirst) throws InterruptedException {
//
//        // printFirst.run() outputs "first". Do not change or remove this line.
//        printFirst.run();
//        flagA =true;
//    }
//
//    public void second(Runnable printSecond) throws InterruptedException {
//        while (!flagA){
//
//        }
//        // printSecond.run() outputs "second". Do not change or remove this line.
//        printSecond.run();
//        flagB = true;
//        flagA = false;
//    }
//
//    public void third(Runnable printThird) throws InterruptedException {
//        while (!flagB){
//
//        }
//        // printThird.run() outputs "third". Do not change or remove this line.
//        printThird.run();
//        flagB = false;
//
//    }

}
